package org.hxyjs.dao.impl;

import org.hxyjs.dao.PressDao;
import org.hxyjs.entity.Press;
import org.hxyjs.entity.Press;
import org.hxyjs.entity.Press;
import org.hxyjs.mapper.PressMapper;
import org.hxyjs.page.PageInfo;
import org.hxyjs.utils.GenNumTools;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository("pressDao")//一般实现类不会直接生成默认对象名 而是使用 接口的默认对象名
public class PressDaoImpl implements PressDao {
    @Resource //autowired 会装配能够直接扫描到的对象  resource spring容器运行的时候才找到需要的对象  注入
    private PressMapper pressMapper;
    public List<Press>  getAllPresss() throws Exception{
        return pressMapper.selectByExample(null);
    }

    @Override
    public Integer deletePressByPrno(String prno)  throws Exception{
        return pressMapper.deleteByPrimaryKey(prno);
    }

    @Override
    public Integer updatePressByPrno(Press press)  throws Exception{
        return pressMapper.updateByPrimaryKey(press);
    }

    @Override
    public Integer addPress(Press press)  throws Exception{
        press.setPrno(initId());
        return pressMapper.insertSelective(press);
    }
    public String initId(){
        String maxPrno = pressMapper.getMaxPrno();
        return GenNumTools.initId("",3,maxPrno);
    }

    @Override
    public List<Press> getPressByPage(PageInfo pageInfo )  throws Exception{
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("currPage",pageInfo.getCurrPage());
        map.put("pageSize",pageInfo.getPageSize());
        int itemCount = (int)pressMapper.countByExample(null);
        pageInfo.setItemCount(itemCount);
        return pressMapper.getPressByPage(map);
    }

    @Override
    public Press selectPressWithSalesagentAndBooks(String prno) throws Exception {
        return pressMapper.selectPressWithSalesagentAndBooks(prno);
    }

    @Override
    public Press getAccountByAccountName(String prname) {
        return pressMapper.getAccountByAccountName(prname);
    }

}
